package com.lanou.horo.zhuizhui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.horo.zhuizhui.R;
import com.lanou.horo.zhuizhui.adapter.EveryDayAdapter;
import com.lanou.horo.zhuizhui.entity.CommunityPageEssenceItem;
import com.lanou.horo.zhuizhui.entity.EveryDayItem;
import com.lanou.horo.zhuizhui.utility.clickInterface.EveryDayClick;
import com.lanou.horo.zhuizhui.utility.recyclerView.SpacesItemDecoration;

import java.util.List;

/**
 * Created by dllo on 15/11/5.
 */
public class EveryDayActivity extends AppCompatActivity implements EveryDayClick {
    private RecyclerView recyclerView;
    private EveryDayAdapter adapter;
    private RequestQueue queue;
    private ImageView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_day_every);
        back = (ImageView) findViewById(R.id.everydayBackBtn);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        recyclerView = (RecyclerView) findViewById(R.id.everyDayRecyclerView);
        adapter = new EveryDayAdapter(this);
        GridLayoutManager manager = new GridLayoutManager(this, 1);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        int spacingInPixels = 20;
        recyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));
        recyclerView.setHasFixedSize(true);
        adapter.setEveryDayClick(this);
        initData();
    }

    private void initData() {
        queue = Volley.newRequestQueue(this);
        String url = "http://api.zhuizhuiyoyo.com/request.php?method=theme_info&timestamp=" +
                "1446721227521&param=%7B%22id%22%3A%223145%22%2C%22v%22%3A1%2C%22t%22%3A0%2C%22" +
                "count%22%3A18%2C%22page%22%3A1%7D&sig=24f94f2eaf4902e568e2f0dc2f292169";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Gson gson = new Gson();
                EveryDayItem edi = gson.fromJson(response, EveryDayItem.class);
                if (edi != null) {
                    List<EveryDayItem.ThemeListEntity> list = edi.getTheme_list();
                    adapter.addList(list);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        queue.add(request);
    }

    @Override
    public void everyDayItemClick(EveryDayItem.ThemeListEntity url) {
        Intent intent = new Intent(this, WebViewActivity.class);
        intent.putExtra("url", url.getUrl());
        intent.putExtra("title", url.getName());
        startActivity(intent);
    }

    @Override
    public void communityItemClick(CommunityPageEssenceItem.TopicsEntity top) {

    }
}
